package com.vf.admin.miaodong.controller;

import com.vf.admin.common.BaseController;
import com.vf.admin.common.Constants;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppMessageRecord;
import com.vf.admin.miaodong.entity.AppPingtaiHuodong;
import com.vf.admin.miaodong.service.AfterPingtaiHuodongService;
import com.vf.admin.miaodong.service.AfterSystemService;
import com.vf.admin.utils.Base64Image.FileUtil;
import com.vf.admin.utils.StringEx;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 平台活动
 *
 * @author anxiang
 */
@CrossOrigin
@Controller
@RequestMapping("/after/pingtai")
public class AfterPingtaiHuodongController extends BaseController {

    @Autowired
    AfterPingtaiHuodongService afterPingtaiHuodongService;

    /**
     * 获取平台活动
     */
    @RequestMapping("/list")
    public String list(SplitPageBean splitPageBean) {
        try {
            Map<String, Object> map = new HashMap<>(16);
            List<AppPingtaiHuodong> appPingtaiHuodongList = afterPingtaiHuodongService.findList(map, splitPageBean);
            request.setAttribute("list", appPingtaiHuodongList);
            request.setAttribute("splitPage", splitPageBean);
            return this.sucess("/web/after/pingtai/list");
        } catch (Exception e) {
            return this.error("系统异常");
        }
    }

    /**
     * 平台活动添加跳转
     *
     * @return
     */
    @RequestMapping("/add")
    public String add() {
        try {
            return this.sucess("/web/after/pingtai/add");
        } catch (Exception e) {
            return this.error("系统异常");
        }
    }

    /**
     * 平台活动编辑跳转
     *
     * @param id id
     * @return
     */
    @RequestMapping("/detail")
    public String detail(Integer id) {
        try {
            AppPingtaiHuodong appMessageRecord = afterPingtaiHuodongService.get(id);
            request.setAttribute("appMessageRecord", appMessageRecord);
            return this.sucess("/web/after/pingtai/edit");
        } catch (Exception e) {
            return this.error("系统繁忙");
        }
    }

    /**
     * 添加平台活动
     *
     * @param huodong 实体类
     */
    @RequestMapping("/save")
    @ResponseBody
    public Result addMessage(AppPingtaiHuodong huodong) {
        try {
            if (StringUtils.isEmpty(huodong.getMessage())) {
                return errorJSON("消息不能为空");
            }
            String filePath = "";
            Map map = FileUtil.aliyunOSSUpload(huodong.getImage(),"");
            if(map.get("st").equals("0")) {
                filePath = (String)map.get("data");
            }
            if(StringEx.isNotNull(filePath)) {
                huodong.setImage(filePath);
            }
            huodong.setMessage(huodong.getMessage());
            huodong.setTitle(huodong.getTitle());
            huodong.setCreatetime(new Date());
            huodong.setState(huodong.getState() == null ? Constants.shiFou_STATE_FOU : huodong.getState());
            afterPingtaiHuodongService.save(huodong);
            return sucessJSON("添加成功");
        } catch (Exception e) {
            System.out.println(e);
            return this.errorJSON("系统繁忙");
        }
    }


}
